package net.sahv.bdyz.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import net.sahv.bdyz.mapper.MemberMapper;
import net.sahv.bdyz.model.Member;
import net.sahv.bdyz.util.PageInfo;

@Service
public class MemberService {

	@Autowired
	private MemberMapper memberMapper;
	
	public Member selectUserMessage(@Param("mPhone")String mPhone){
		return memberMapper.selectUserMessage(mPhone);
	}
	
	public int insert(Member record){
		return memberMapper.insert(record);
	}
	
	public int updateByPrimaryKey(Member record) {
		return memberMapper.updateByPrimaryKey(record);
	}
	
	public int updatePassword(@Param("mPassword")String mPassword,@Param("mPhone")String mPhone){
		return memberMapper.updatePassword(mPassword, mPhone);
	}
	
	public Member selectByPrimaryKey(Integer mId) {
		return memberMapper.selectByPrimaryKey(mId);
	}
	
	public int updateUserMessage(@Param("mNickname")String mNickname,@Param("mEmail")String mEmail,@Param("mFixedTelephone")String mFixedTelephone,
    		@Param("mImg")String mImg,@Param("mId")Integer mId,@Param("mPassword")String mPassword,@Param("mProvince")String mProvince,@Param("mCity")String mCity,@Param("mArea")String mArea){
		return memberMapper.updateUserMessage(mNickname, mEmail, mFixedTelephone, mImg, mId,mPassword,mProvince,mCity,mArea);
	}
	
	public List<Map<String, Object>> selectAlls(PageInfo pageInfo, String where, String order) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("myCount", pageInfo.getCount());
		param.put("pageSize", pageInfo.getPageSize());
		param.put("page", pageInfo.getPage());
		param.put("where", where);
		param.put("order", order);
		List<Map<String, Object>> m = memberMapper.selectAlls(param);
		pageInfo.setCount(Integer.parseInt(param.get("myCount").toString()));
		return m;
	}
	
	/**
	 * 查询咨询列表数据
	 * @param param
	 * @return
	 * 张羽鹏
	 * 2016年8月6日14:12:40
	 */
	public List<Map<String, Object>> memberList(Map<String, Object> param) {
		return memberMapper.memberList(param);
	}
	/**
	 * 查询资讯列表数据的数量
	 * @param where
	 * @return
	 * 张羽鹏
	 * 2016年8月6日14:13:08
	 */
	public Integer memberListCount(String where) {
		return memberMapper.memberListCount(where);
	}
	
	public  int deleteByPrimaryKey(Integer mId){
		return memberMapper.deleteByPrimaryKey(mId);
	}

	/**
	 * @author 作者 : 任凯伟
	 * @date 创建时间：2016年8月10日 上午11:14:12
	 * @version 1.0
	 * @see 查询当前登录会员信息
	 * @param
	 * @parameter
	 * @since
	 * @return
	 */
	public Member getCurrent() {
		RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
		if (requestAttributes != null) {
			HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
			Integer mId = (Integer) request.getSession().getAttribute("mId");
			if (mId != null) {
				return memberMapper.selectByPrimaryKey(mId);
			}
		}
		return null;
	}

	/**
	 * @author 作者 : 任凯伟
	 * @date 创建时间：2016年8月17日 上午9:25:14
	 * @version 1.0
	 * @see
	 * @param
	 * @parameter
	 * @since
	 * @return
	 */
	public Member findByTel(String telphone) {
		return memberMapper.findByTel(telphone);
	}

	public Member selectByPrimaryKeyNew(Integer mId) {
		// TODO Auto-generated method stub
		return memberMapper.selectByPrimaryKeyNew(mId);
	}

	/**
	 * 用户所在省区列表
	 * @author 张天敏
	 * @date 日期：2016年9月26日 时间：下午3:13:54
	 * @param param
	 * @return
	 */
	public List<Map<String, Object>> memberProvinceList(Map<String, Object> param) {
		return memberMapper.memberProvinceList(param);
	}

	/**
	 * 用户所在省区列表统计
	 * @author 张天敏
	 * @date 日期：2016年9月26日 时间：下午3:14:02
	 * @param param
	 * @return
	 */
	public Integer memberProvinceListCount(Map<String, Object> param) {
		return memberMapper.memberProvinceListCount(param);
	}
}
